﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Reflection;
using System.Text;
using System.IO; 

namespace Sharp
{
    
    public class SiteMap : Handler
    {
		[HttpHandler("Sitemap")]
        public void ShowSiteMap()
        { 
            context.Response.ContentType = "text/html";
            this.data = BuildHandlers(context);
        }


        private  string BuildHandlers(HttpContext context)
        {
            StringBuilder SiteMap = new StringBuilder();
            SiteMap.Append("<h2>SiteMap</h2>");

            foreach (var ass in AppDomain.CurrentDomain.GetAssemblies())
            {
                bool flag = false;
                StringBuilder Handlers = new StringBuilder();

                foreach (Type type in ass.GetTypes())
                {
                    if (typeof(Handler).IsAssignableFrom(type))
                    {
                        //Class endpoint
                        if (type.GetCustomAttributes(typeof(HttpHandler), true).Length == 1)
                        {
                            AddRoute(type, type, Handlers);
                            flag = true;
                        }

                        // Method endpoint
                        foreach (var method in type.GetMethods())
                        {
                            if (method.GetCustomAttributes(typeof(HttpHandler), false).Length == 1)
                            {
                                AddRoute(type, method, Handlers);
                                flag = true;
                            }
                        }
                    }
                }

                if (flag)
                {
                    SiteMap.AppendFormat("<b>{0}</b><br>", ass.FullName);
                    SiteMap.Append("<ul>" + Handlers.ToString() + "</ul>");
                }
            }
            
          
            return SiteMap.ToString();
        }

        private static void AddRoute(Type type, object handlerType, StringBuilder Handlers)
        {
            HttpHandler wp;
            string name;
            if (handlerType is MethodInfo)
            {
                wp = (HttpHandler)((MethodInfo)handlerType).GetCustomAttributes(typeof(HttpHandler), true)[0];
                name = type.FullName + " - " + ((MethodInfo)handlerType).Name;
            }
            else
            {
                wp = (HttpHandler)((Type)handlerType).GetCustomAttributes(typeof(HttpHandler), true)[0];
                name = ((Type)handlerType).FullName;
            }


            Handlers.AppendLine(String.Format(@"<li><a href=""/{0}{3}"">/{0} - {2}</a> {1}</li>",
                                    wp.Url,
                                    (wp.Type).ToStringValue(),
                                    name,
                                    wp.Verb == HttpHandler.HTTPVerb.POST ? "?test=test" : ""
                                    ));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
